package cc.wanforme.chipmunity.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.chipmunity.common.bean.WebResponse;
import cc.wanforme.chipmunity.controller.base.BaseController;
import cc.wanforme.chipmunity.system.po.Passage;
import cc.wanforme.chipmunity.system.service.interfaces.PassageService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author wanne
 * 2019年8月17日
 * 
 */
@Controller
@RequestMapping("/passage")
public class PassageController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(PassageController.class);
	
	
	@Autowired
	private PassageService passageService;
	
	@RequestMapping("/add")
	@ResponseBody
	@ApiOperation("添加新文章")
	@ApiImplicitParam(name="Passage", dataType="passage", value="新文章", required=true)
	@PreAuthorize("hasAuthority('passage:add')")
	public WebResponse addPassage(@RequestBody(required=true) Passage passage) {
		try {
			return passageService.savePassage(passage);
		} catch (Exception e) {
			log.error("添加文章", e);
			return WebResponse.fail("发送失败！服务器异常！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@ApiOperation("更新文章")
	@ApiImplicitParam(name="Passage", dataType="passage", value="根据id更新文章，"
			+ "不更新subjectId、title、authorId、author、publishTime", required=true)
	@PreAuthorize("hasAuthority('passage:update')")
	public WebResponse updatePassage(@RequestBody(required=true) Passage passage) {
		try {
			return passageService.updatePassage(passage);
		} catch (Exception e) {
			log.error("更新文章", e);
			return WebResponse.fail("保存失败！服务器异常！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	@ApiOperation("更新文章")
	@ApiImplicitParam(name="id", dataType="Integer", value="删除文章", required=true)
	@PreAuthorize("hasAuthority('passage:delete')")
	public WebResponse deletePassage(@RequestParam(required=true) Long id) {
		try {
			return passageService.deletePassage(id);
		} catch (Exception e) {
			log.error("更新（删除）文章", e);
			return WebResponse.fail("删除失败！服务器异常！");
		}
	}
	
}
